package com.example.demo.interceptor;

import com.example.demo.entity.Admin;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * @author shenrui
 * @date 2021/9/19
 * @description 对admin登录时的拦截器，继承HandlerInterceptor
 */
public class AdminLoginInterceptor implements HandlerInterceptor {
    /**
     *@description 在进入前判断用户是否登录
     * @return bolean
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Admin admin = (Admin) request.getSession().getAttribute("adminLogin");
        if(admin != null) {
            return true;
        } else {
            request.setAttribute("adminMsg","请先登录admin");
            request.getRequestDispatcher("/admin_login").forward(request,response);
            return false;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
